package com.haizhi.crm.biz.task.message;


import com.haizhi.crm.biz.model.MessageSourceDo;
import com.haizhi.crm.biz.service.MessageSourceService;
import com.haizhi.crm.biz.task.message.engine.MessageProducer;
import com.haizhi.crm.biz.task.message.model.ProcessResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Future;

/**
 * Created by wangshan on 2018/5/16.
 */
@Service
public class MessageProduceTask {

    @Autowired
    private MessageSourceService messageSourceService;

    @Autowired
    private MessageProducer messageProducer;

    @Value("${message.producer.cron}" )
    private String cron;

    @Scheduled(cron = "0 0 3 * * ?" )
    public void run() {
        List<MessageSourceDo> messageSourceDoList = messageSourceService.query();
        for (MessageSourceDo messageSourceDo : messageSourceDoList) {
            handler(messageSourceDo);
        }
    }

    @Async
    public Future<ProcessResp> handler(MessageSourceDo messageSourceDo) {
        return new AsyncResult<>(messageProducer.produce(messageSourceDo));
    }
}
